from flask import Blueprint,render_template,request,jsonify
import BaseDao
import re
blu=Blueprint("signin",__name__)


@blu.route('/signin', methods=['post', 'get'])  # 注册
def signin():
    return render_template('signin.html')


@blu.route('/signin/run', methods=['post', 'get'])  # 处理注册
def signin_run():
    name = request.form.get('username')
    pwd = request.form.get('pwd')
    password = request.form.get('password')
    email = request.form.get('email')
    status = {'status': 200}
    if re.findall('^[a-zA-Z0-9_]{3,20}$', name):
        if re.findall('^[a-zA-Z0-9_]{6,20}$', pwd):
            if password == pwd:
                sql_check = 'select * from user where username="{}"'.format(name)
                if not (BaseDao.select_db(sql_check)):
                    sql = "insert into user values (null,'{}','{}','{}')".format(name, pwd, email)
                    if BaseDao.change_db(sql):
                        pass
                    else:
                        status['status'] = 2
                else:
                    status['status'] = 3
            else:
                status['status'] = 4
        else:
            status['status'] = 5
    else:
        status['status'] = 6
    # print(status)
    return jsonify(status)